<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>apache几种限制ip的方法 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>apache几种限制ip的方法</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<ol>
<li>禁止访问某些文件/目录增加Files选项来控制，比如要不允许访问 .inc 扩展名的文件，保护php类库：</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">&lt;Files ~ <span class="string">"\.inc$"</span>&gt;</div><div class="line"> Order allow,deny</div><div class="line"> Deny from all</div><div class="line">&lt;/Files&gt;</div></pre></td></tr></table></figure>
<ol>
<li>禁止访问某些指定的目录：（可以用 <directorymatch> 来进行正则匹配）</directorymatch></li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">&lt;Directory ~ <span class="string">"^/var/www/（.+/）*[0-9]&#123;3&#125;"</span>&gt;</div><div class="line"> Order allow,deny</div><div class="line"> Deny from all</div><div class="line">&lt;/Directory&gt;</div></pre></td></tr></table></figure>
<ol>
<li>通过文件匹配来进行禁止，比如禁止所有针对图片的访问：</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">&lt;FilesMatch \.(gif|jpeg|png)$&gt;</div><div class="line"> Order allow,deny</div><div class="line"> Deny from all</div><div class="line">&lt;/FilesMatch&gt;</div></pre></td></tr></table></figure>
<ol>
<li>针对URL相对路径的禁止访问：</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">&lt;Location /dir/&gt;</div><div class="line"> Order allow,deny</div><div class="line"> Deny from all</div><div class="line">&lt;/Location&gt;</div></pre></td></tr></table></figure>
<ol>
<li>禁止某些IP访问/只允许某些IP访问如果要控制禁止某些非法IP访问，在Directory选项控制：</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">&lt;Directory <span class="string">"/var/www/web/"</span>&gt;</div><div class="line"> Order allow,deny</div><div class="line"> Allow from all</div><div class="line"> Deny from 10.0.0.1          <span class="comment">#阻止一个IP</span></div><div class="line"> Deny from 192.168.0.0/24    <span class="comment">#阻止一个IP段</span></div><div class="line">&lt;/Directory&gt;</div></pre></td></tr></table></figure>
<ol>
<li>只允许某些IP访问，适合比如就允许内部或者合作公司访问：</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">&lt;Directory <span class="string">"/var/www/web/"</span>&gt;</div><div class="line"> Order deny,allow</div><div class="line"> Deny from all</div><div class="line"> Allow from example.com              <span class="comment">#允许某个域名</span></div><div class="line"> Allow from 10.0.0.1                 <span class="comment">#允许一个IP</span></div><div class="line"> Allow from 10.0.0.1 10.0.0.2        <span class="comment">#允许多个IP</span></div><div class="line"> Allow from 10.1.0.0/255.255.0.0     <span class="comment">#允许一个IP段，掩码对</span></div><div class="line"> Allow from 10.0.1 192.168           <span class="comment">#允许一个IP段，后面不填写</span></div><div class="line"> Allow from 192.168.0.0/24           <span class="comment">#允许一个IP段，网络号</span></div><div class="line">&lt;/Directory&gt;</div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/Apache/46. apache几种限制ip的方法/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
